#include <QApplication>
#include <QMainWindow>
#include <QTextEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QSpinBox>
#include <QCheckBox>
#include <QRandomGenerator>

class PoetryGenerator : public QMainWindow {
    Q_OBJECT
public:
    PoetryGenerator(QWidget *parent = nullptr) : QMainWindow(parent) {
        setupUI();
        initWordBank();
        initRhymeBank();
    }

private:
    void setupUI() {
        // 界面组件
        QWidget *centralWidget = new QWidget(this);
        QVBoxLayout *mainLayout = new QVBoxLayout;

        QHBoxLayout *controlLayout = new QHBoxLayout;
        QLabel *label = new QLabel("诗歌行数:");
        spinBox = new QSpinBox;
        spinBox->setRange(4, 100);
        spinBox->setValue(8);

        rhymeCheck = new QCheckBox("启用押韵");
        generateBtn = new QPushButton("生成诗歌");

        controlLayout->addWidget(label);
        controlLayout->addWidget(spinBox);
        controlLayout->addWidget(rhymeCheck);
        controlLayout->addWidget(generateBtn);

        outputArea = new QTextEdit;
        outputArea->setReadOnly(true);

        mainLayout->addLayout(controlLayout);
        mainLayout->addWidget(outputArea);
        centralWidget->setLayout(mainLayout);
        setCentralWidget(centralWidget);

        connect(generateBtn, &QPushButton::clicked, this, &PoetryGenerator::generatePoetry);
    }

    void initWordBank() {
        nouns = {"时间", "宇宙", "墓碑", "量子", "飞船", "文明", "月光", "维度"};
        verbs = {"凝固", "撕裂", "歌唱", "折叠", "坠落", "闪烁", "湮灭"};
        adjectives = {"透明的", "螺旋的", "熵增的", "递归的", "坍缩的", "量子化的"};
        adverbs = {"寂静地", "悖论般地", "在事件视界", "以光速"};
    }

    void initRhymeBank() {
        rhymes = {"a", "o", "e", "ai", "ei"}; // 简化韵母
    }

private slots:
    void generatePoetry() {
        outputArea->clear();
        QStringList poem;
        QString currentRhyme;

        if(rhymeCheck->isChecked()) {
            currentRhyme = rhymes[QRandomGenerator::global()->bounded(rhymes.size())];
        }

        for(int i=0; i<spinBox->value(); ++i) {
            QString line = generateLine(i, currentRhyme);
            poem << line;
        }

        outputArea->setText(poem.join("\n"));
    }

private:
    QString generateLine(int lineNum, QString rhyme) {
        // 语法模板
        QVector<QString> patterns = {
            "[形容词][名词][动词][名词]",
            "[副词][动词][名词]",
            "[名词]在[动词]",
            "[形容词][名词]的[名词]"
        };

        QString pattern = patterns[QRandomGenerator::global()->bounded(patterns.size())];

        return pattern.replace("[形容词]", randomWord(adjectives))
            .replace("[名词]", randomWord(nouns))
            .replace("[动词]", randomWord(verbs))
            .replace("[副词]", randomWord(adverbs))
            .append(handleRhyme(lineNum, rhyme));
    }

    QString randomWord(const QStringList &wordList) {
        return wordList[QRandomGenerator::global()->bounded(wordList.size())];
    }

    QString handleRhyme(int lineNum, QString rhyme) {
        if(!rhyme.isEmpty() && (lineNum+1)%4 == 0) { // 每四行押韵
            return " (" + rhyme + ")"; // 此处应实现真正的押韵检测
        }
        return "";
    }

    // UI组件
    QSpinBox *spinBox;
    QCheckBox *rhymeCheck;
    QPushButton *generateBtn;
    QTextEdit *outputArea;

    // 词库
    QStringList nouns;
    QStringList verbs;
    QStringList adjectives;
    QStringList adverbs;
    QStringList rhymes;
};

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    PoetryGenerator w;
    w.resize(600, 400);
    w.show();
    return a.exec();
}


